const {HttpException} = require('../core/http-exception')

const catchError = async (ctx, next) => {
    try {
        await next()

    } catch (error) {
        // 开发环境
        const isHttpException = error instanceof HttpException;
        console.log('进入报错专用Js了,exception.js,该错误可在F12的network里查看')
        console.log('isHttpException的值:::::', isHttpException)

        // 属于自定义错误集里
        if (isHttpException) {
            ctx.response.status = error.code
            ctx.body = {
                msg: error.msg,
                errCode: error.errCode,
                request: `${ctx.method} ${ctx.path}`
            }
        } else {
            console.log('--------------------------------------------------------------------')
            console.log('-----------------------------给node控制台看@begin-----------------------')
            console.log('--------------------------------------------------------------------')
            console.log(error)
            console.log('--------------------------------------------------------------------')
            console.log('-----------------------------给node控制台看@end--------------------------')
            console.log('--------------------------------------------------------------------')
            ctx.response.status = 500
            ctx.body = {
                msg: "未知错误！",
                errCode: -9999,
                request: `${ctx.method} ${ctx.path}`,
                err: error
            }
            //如果是sql处理错误,error会有对应的错误描述,如果是node代码错误,则为空对象,需在cmd里看错误信息
        }
    }
}

module.exports = catchError
